import request from './request'
import type { LoginForm, UserInfo } from '@/types/user'

// 用户登录
export const login = (data: LoginForm) => {
  // OAuth2PasswordRequestForm 需要 form-data 格式
  const formData = new FormData()
  formData.append('username', data.username)
  formData.append('password', data.password)
  
  return request({
    url: '/auth/login',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 获取用户信息
export const getUserInfo = (): Promise<UserInfo> => {
  return request({
    url: '/auth/me',
    method: 'get'
  })
}

// 用户登出
export const logout = () => {
  return request({
    url: '/auth/logout',
    method: 'post'
  })
}
